<template>
  <div>
    <Chart :cdata="cdata" />
  </div>
</template>

<script>
import Chart from './chart.vue'
export default {
  data () {
    return {
      drawTiming: null,
      cdata: {
        year: null,
        weekCategory: [],
        maxData: 40000,
        weekMaxData: [],
        weekLineData: [],
        weekLineData2: [],
        weekLineData3: [],
        weekLineData4: []
      }
    }
  },
  components: {
    Chart,
  },
  mounted () {
    this.getdata();
  },
  beforeDestroy () {
    clearInterval(this.drawTiming);
  },
  methods: {
    drawTimingFn () {
      this.setData();
      this.drawTiming = setInterval(() => {
        this.setData();
      }, 6000);
    },
    setData () {
      // 清空轮询数据
      this.cdata.weekCategory = [];
      this.cdata.weekMaxData = [];
      this.cdata.weekLineData = [];

      let dateBase = new Date();
      this.cdata.year = dateBase.getFullYear();
      // 周数据
      for (let i = 0; i < 7; i++) {
        // 日期
        let date = new Date();
        this.cdata.weekCategory.unshift([date.getMonth() + 1, date.getDate() - i].join("/"));

        // 折线图数据
        this.cdata.weekMaxData.push(this.cdata.maxData);

        let distance = Math.round(Math.random() * 11000 + 500);
        this.cdata.weekLineData.push(distance);

        distance = Math.round(Math.random() * 11000 + 600);
        this.cdata.weekLineData2.push(distance);

        distance = Math.round(Math.random() * 11000 + 700);
        this.cdata.weekLineData3.push(distance);

        distance = Math.round(Math.random() * 11000 + 800);
        this.cdata.weekLineData4.push(distance);
      }

    },
    async getdata(){
      let self = this;
      this.$http.get('http://116.196.92.193:8090/data/getBottomRight', {
        params: {
        }
      }).then(function (response) {
        //console.info(response);
        var arr = response.data.data;
        for(var i = 0;i<arr.length;i++){

          var cases = arr[i].cases;
          var deaths = arr[i].deaths;
          var time = arr[i].time;

          let date = new Date(time);
          var YY = date.getFullYear() + '-';
          var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
          var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());

          self.cdata.weekCategory.unshift(YY+MM+DD);
          self.cdata.weekMaxData.push(self.cdata.maxData);
          self.cdata.weekLineData.unshift(cases);
          self.cdata.weekLineData2.unshift(deaths);

        }

      }).catch(function (error) {
        console.log(error);
      });

      this.$http.get('http://116.196.92.193:8090/data/getPredictDatas', {
        params: {
        }
      }).then(function (response) {
        //console.info(response);
        var arr = response.data.data.casePredict;
        var arr2 = response.data.data.deathPredict;
        for(var i = 0;i<arr.length;i++){

          var cases = arr[i].value;
          var deaths = arr2[i].value;

          self.cdata.weekLineData3.unshift(cases);
          self.cdata.weekLineData4.unshift(deaths);
        }

      }).catch(function (error) {
        console.log(error);
      });
    },
  }
};
</script>

<style lang="scss" scoped>
</style>
